package com.cqc.other.designPattern.strategyPattern.strategy2;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class FileStrategyFactory {

    private static Map<FileStrategyEnum, FileStrategy> strategyMap = new HashMap<FileStrategyEnum, FileStrategy>();

    static {
        strategyMap.put(FileStrategyEnum.CSV, new CsvFileStrategy());
        strategyMap.put(FileStrategyEnum.TXT, new TxtFileStrategy());
        strategyMap.put(FileStrategyEnum.XLS, new XlsFileStrategy());
    }

    public FileStrategy getFileStrategy(File file) {
        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf(".");
        String suffix = fileName.substring(dotIndex + 1);

        FileStrategyEnum fileStrategyEnum = FileStrategyEnum.valueOf(suffix.toUpperCase());
        TxtFileStrategy defaultStrategy = new TxtFileStrategy();
        return strategyMap.getOrDefault(fileStrategyEnum, defaultStrategy);
    }


}
